

/*=================== 下面是非命名空间的方法，一个页面只能有一个分页 ============================*/

function queryListSearch(searchJSON){
	if(isSet(window.split_property)){
		// 保存其它搜索条件，并把分页信息清空
		window.split_property = $.extend(window.split_property, {orderBy:"", gopage:1, rownum:"", currentpage:1});
	}

	queryListAjax(window.action_target, searchJSON, splitPageJSON.pageId, null);
}

function queryListAjax(targetAction, dataJson, pageId, showProccessor){
	if(isSet(showProccessor)){
		window.showProccessor = showProccessor;
	}

	if(isSet(window.split_property)){
		dataJson = $.extend(window.split_property, dataJson);
	}
	//alert(JSON.stringify(dataJson));
	$.ajax({
		   url: targetAction,
		   async: false,
		   type: 'POST',
		   data: dataJson,
		   complete:function(XMLHttpRequest, textStatus){
			//alert(XMLHttpRequest.responseText); 
		 },
		   success: function(msg){
			   if(isSet(msg)){
				   if(isQuerySplitInt(msg.startRowNum)){
					   window.startRowNumAjax = parseInt(msg.startRowNum);
				   }else{
					   window.startRowNumAjax = 0;
				   }
				   
				   generateSplitPage(msg.splitPageJSON, pageId, targetAction);
				   if(isSet(window.showProccessor)){
					window.showProccessor(msg.list, msg.splitPageJSON);
				   }
			   }
			}
		});
}

function goPage(pageNum){
	var splitPageJSON = window.splitPageJSON;
	var json = {};
	
	if(!isSet(pageNum)){
		if(isNull($('#gopage').val())){
			alert("没有输入页码");
			return;
		}else{
			if(!isQuerySplitInt($('#gopage').val())){
				alert("页码格式错误，请检查是否为整数");
				return;
			}else{
				var gopage = parseInt($('#gopage').val());
				if(gopage < 1 || gopage > parseInt(splitPageJSON.total_pages)){
					alert("对不起，你输入的页码超出范围，请确认后再提交");
				}else{
					pageNum = $('#gopage').val();
				}
			}
		}
	}
	
	json.gopage = pageNum;
	json.currentpage = splitPageJSON.current_page;
	json.rownum = splitPageJSON.total_rows;

	queryListAjax(window.action_target, json, splitPageJSON.pageId, null);
}

function generateSplitPage(splitPageJSON, pageId, targetAction){
	if(!isSet(splitPageJSON)){	//防止为空时报错
		splitPageJSON = {};
	}
	splitPageJSON.pageId = pageId;
	window.splitPageJSON = splitPageJSON;
	window.split_property = splitPageJSON.property;
	window.action_target = targetAction;

	if(isSet(pageId)){	//有分页ID时才处理
		var s = "";
		s += "第 " + splitPageJSON.current_page + " 页&nbsp;&nbsp;共" + splitPageJSON.total_pages + "页" + splitPageJSON.total_rows + "条信息";
		s += "&nbsp;&nbsp;&nbsp;&nbsp; <span onclick=\"javascript:goPage('1');\" style=\"cursor:pointer\">首页</span>";
		if(splitPageJSON.hasPrevious == "true"){
			s += "  |  <span onclick=\"javascript:goPage('" + splitPageJSON.previousPage + "');\" style=\"cursor:pointer\">上一页</span>";
		}else{
			s += "  |  <span style=\"color: #999999\">上一页</span>";
		}
		if(splitPageJSON.hasNext == "true"){
			s += "  |  <span onclick=\"javascript:goPage('" + splitPageJSON.nextPage + "');\" style=\"cursor:pointer\">下一页</span>";
		}else{
			s += "  |  <span style=\"color: #999999\">下一页</span>";
		}
		
		s += "  |  <span onclick=\"javascript:goPage('" + splitPageJSON.total_pages + "');\" style=\"cursor:pointer\">尾页</span>";
		
		s += "&nbsp;&nbsp; 请输入页码:  <input onkeydown=\"javascript:if(event.keyCode == 13){goPage(null);}\" type=\"text\" size=\"3\" name=\"gopage\" id=\"gopage\" value=\"\" class=\"standardInput\" style=\"width:50px;\" />";
		
		s += "&nbsp;<input type=\"button\" value=\"提 交\" class=\"standardBtn\" onclick=\"javascript:goPage(null);\" />";

		$('#' + pageId).html(s);
	}
}

/*=================== 下面是使用命名空间的方法，支持一个页面多个分页 ============================*/

function queryListSearchByName(searchJSON, namespace){
	eval("if(!isSet(window." + namespace + ")){window." + namespace + " = {};}");	//命名空间初始化;防止下面使用时报脚本错误

	//取得分页参数 及 分页链接
	eval("var split_property = window." + namespace + ".split_property;");
	eval("var action_target = window." + namespace + ".action_target;");

	if(isSet(split_property)){
		// 保存其它搜索条件，并把分页信息清空
		eval("window." + namespace + ".split_property = $.extend(split_property, {orderBy:'', gopage:1, rownum:'', currentpage:1});");
	}

	queryListAjaxByName(action_target, searchJSON, splitPageJSON.pageId, null, namespace);
}

function queryListAjaxByName(targetAction, dataJson, pageId, showProccessor, namespace){
	eval("if(!isSet(window." + namespace + ")){window." + namespace + " = {};}");	//命名空间初始化;防止下面使用时报脚本错误

	if(isSet(showProccessor)){	//是否已经设置过分页处理方法
		eval("window." + namespace + ".showProccessor = showProccessor;");
	}

	eval("var split_property = window." + namespace + ".split_property;");
	eval("var winShowProccessor = window." + namespace + ".showProccessor;");

	if(isSet(split_property)){
		dataJson = $.extend(split_property, dataJson);
	}
	//alert(JSON.stringify(dataJson));
	$.ajax({
		   url: targetAction,
		   async: false,
		   type: 'POST',
		   data: dataJson,
		   success: function(msg){
			   if(isSet(msg)){
				   if(isQuerySplitInt(msg.startRowNum)){	// 设置分页开始行 数目
				   		eval("window." + namespace + ".startRowNumAjax = parseInt(msg.startRowNum);");
					}else{
						eval("window." + namespace + ".startRowNumAjax = 0;");
					}
				   
				   generateSplitPageByName(msg.splitPageJSON, pageId, targetAction, namespace);
				   if(isSet(winShowProccessor)){
					 	winShowProccessor(msg.list, msg.splitPageJSON, namespace);
				   }
			   }
			}
		});
}

function goPageByName(pageNum, namespace){
	eval("if(!isSet(window." + namespace + ")){window." + namespace + " = {};}");	//命名空间初始化;防止下面使用时报脚本错误
	
	eval("var splitPageJSON = window." + namespace + ".splitPageJSON;");	//分页参数
	eval("var action_target = window." + namespace + ".action_target;");	//分页链接

	var json = {};
	
	if(!isSet(pageNum)){
		if(isNull($('#gopage').val())){
			alert("没有输入页码");
			return;
		}else{
			if(!isQuerySplitInt($('#gopage').val())){
				alert("页码格式错误，请检查是否为整数");
				return;
			}else{
				var gopage = parseInt($('#gopage').val());
				if(gopage < 1 || gopage > parseInt(splitPageJSON.total_pages)){
					alert("对不起，你输入的页码超出范围，请确认后再提交");
				}else{
					pageNum = $('#gopage').val();
				}
			}
		}
	}
	
	json.gopage = pageNum;
	json.currentpage = splitPageJSON.current_page;
	json.rownum = splitPageJSON.total_rows;

	queryListAjax(action_target, json, splitPageJSON.pageId, null);
}

function generateSplitPageByName(splitPageJSON, pageId, targetAction, namespace){
	eval("if(!isSet(window." + namespace + ")){window." + namespace + " = {};}");	//命名空间初始化;防止下面使用时报脚本错误

	if(!isSet(splitPageJSON)){	//防止为空时报错
		splitPageJSON = {};
	}
	splitPageJSON.pageId = pageId;

	//设置分页参数为全局变量
	eval("window." + namespace + ".splitPageJSON = splitPageJSON;");
	eval("window." + namespace + ".split_property = splitPageJSON.property;");
	eval("window." + namespace + ".action_target = targetAction;");

	if(isSet(pageId)){	//有分页ID时才处理
		var s = "";
		s += "第 " + splitPageJSON.current_page + " 页&nbsp;&nbsp;共" + splitPageJSON.total_pages + "页" + splitPageJSON.total_rows + "条信息";
		s += "&nbsp;&nbsp;&nbsp;&nbsp; <span onclick=\"javascript:goPage('1');\" style=\"cursor:pointer\">首页</span>";
		if(splitPageJSON.hasPrevious == "true"){
			s += "  |  <span onclick=\"javascript:goPage('" + splitPageJSON.previousPage + "');\" style=\"cursor:pointer\">上一页</span>";
		}else{
			s += "  |  <span style=\"color: #999999\">上一页</span>";
		}
		if(splitPageJSON.hasNext == "true"){
			s += "  |  <span onclick=\"javascript:goPage('" + splitPageJSON.nextPage + "');\" style=\"cursor:pointer\">下一页</span>";
		}else{
			s += "  |  <span style=\"color: #999999\">下一页</span>";
		}
		
		s += "  |  <span onclick=\"javascript:goPage('" + splitPageJSON.total_pages + "');\" style=\"cursor:pointer\">尾页</span>";
		
		s += "&nbsp;&nbsp; 请输入页码:  <input onkeydown=\"javascript:if(event.keyCode == 13){goPage(null);}\" type=\"text\" size=\"3\" name=\"gopage\" id=\"gopage\" value=\"\" class=\"standardInput\" style=\"width:50px;\" />";
		
		s += "&nbsp;<input type=\"button\" value=\"提 交\" class=\"standardBtn\" onclick=\"javascript:goPage(null);\" />";

		$('#' + pageId).html(s);
	}
}

/*=================== 下面是公用方法 ============================*/
function isQuerySplitInt(s){
	var patrn=/^\d+$/; 
	if (!patrn.exec(s)){
		return false;
	}
	
	return true;
}